﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text;
using System.Text.RegularExpressions;

using System.IO;

using System.Windows.Forms;
using System.Drawing;


namespace japanese
{
    class LogFile {
        public const string logfile = "log.txt";

    }

    //一个excel文件的记录
    class LogExcel{
        //文件名
        public String filename;
        public List<LogSheetItem>sheets;

        public int RecoverLog()
        {
            return 0;
        }

        public int SaveLog()
        {

            return 0;
        }
    }


    //一个sheet 文件的记录
    class LogSheetItem {
        public int SheetIndex; //页面索引
        public int SheetOffset;//页内偏移

        public LogSheetItem() {
            this.SheetIndex = 1;
            this.SheetOffset = 1;
        }
        public LogSheetItem(int sheet) { this.SheetIndex = sheet; this.SheetOffset = 1;}
    }


    class LogSetting {

        public LogSettingDisplay display; //显示方式 
        public LogSettingMode mode;   //阅读模式 
        public LogSettingTimes times; //定时阅读
        public String  backcolor;      //背景颜色

        public String font;            //字体类型
        public float fontsize;         //字体大小
        public String fontcolor;       //字体颜色


        //默认颜色  
        public static String DefaultBackColor = "ffd2b78c";
        public static String DefaultFontStyle = "仿宋";
        public static float DefaultFontSize = 12.5F;
        public static String DefaultFontColor = "ffffffff";

        public int logline;

        public Color ToBackGroundColor()
        {
            try
            {
                return Color.FromArgb(int.Parse(this.backcolor,System.Globalization.NumberStyles.HexNumber));
            }
            catch {
                return Color.FromArgb(int.Parse(LogSetting.DefaultBackColor,System.Globalization.NumberStyles.HexNumber));
            }
            
        }

        public Color ToFontColor()
        {
            try {
                return Color.FromArgb(int.Parse(this.fontcolor, System.Globalization.NumberStyles.HexNumber));
            }
            catch {
                return Color.FromArgb(int.Parse(LogSetting.DefaultFontColor, System.Globalization.NumberStyles.HexNumber));
            }
        }

        public LogSetting()
        { 
            //默认
            display = LogSettingDisplay.HanZiFirst;
            mode = LogSettingMode.SequenceRecite;
            times = LogSettingTimes.Disable;
            this.logline = 0;
            this.logSettingRecover();

   
        }


        public int logSettingRecover()
        {
            StreamReader rd;
            String fn = System.IO.Directory.GetCurrentDirectory() + System.IO.Path.DirectorySeparatorChar + LogFile.logfile;
            try
            {
                
                rd = new StreamReader(fn, Encoding.Default);
            }
            catch (Exception e)
            {
                System.Console.WriteLine("logSettingRecover无法打开:" + fn);
                MessageBox.Show("logSettingRecover无法打开文件" + fn);
                return -1;
            }

            this.logline = 0;
            String str;
            bool exist = false;
            for (str = rd.ReadLine(); str != null; str = rd.ReadLine())
            {
                this.logline++;
                Match m = Regex.Match(str, @"setting\[(\w+)\]\[(\w+)\]\[(\w+)\]\[(\w+)\]\[(\w+)\]\[([^[]*[^]])\]\[([^[]*[^]])\]");
                if (m.Groups.Count>1)
                {
                    this.display = (LogSettingDisplay)Enum.Parse(typeof(LogSettingDisplay), ((m.Groups[1].Value == "" )? ("JiaMingFirst") : (m.Groups[1].Value)));
                    this.mode = (LogSettingMode)Enum.Parse(typeof(LogSettingMode), ((m.Groups[2].Value == "") ? ("RandRecite") : (m.Groups[2].Value)));
                    this.times = (LogSettingTimes)Enum.Parse(typeof(LogSettingTimes), ((m.Groups[3].Value == "") ? ("Disable") : (m.Groups[3].Value)));
                    this.backcolor = (m.Groups[4].Value == "") ? (LogSetting.DefaultBackColor) : (m.Groups[4].Value);


                    this.font = (m.Groups[5].Value == "") ? (LogSetting.DefaultFontStyle) : (m.Groups[5].Value);
                    this.fontsize = (m.Groups[6].Value == "") ? (LogSetting.DefaultFontSize) : (float.Parse( m.Groups[6].Value));
                    this.fontcolor = (m.Groups[7].Value == "") ? (LogSetting.DefaultFontColor) : (m.Groups[7].Value);

                    if (this.times != LogSettingTimes.Disable) this.times = LogSettingTimes.Disable;//软件打开不能是自动播放

                    exist = true;

                    break;
                }
            }

            if (exist == false)
            { 
                //使用default setting
                this.display = LogSettingDisplay.JiaMingFirst;
                this.mode = LogSettingMode.RandBrouse;
                this.times = LogSettingTimes.Disable;
                this.logline = this.logline;//追加
                this.font = LogSetting.DefaultFontStyle;
                this.fontsize = LogSetting.DefaultFontSize;
                this.backcolor = LogSetting.DefaultBackColor;
                this.fontcolor = LogSetting.DefaultFontColor;
            
            }

            rd.Close();
            return 0;
        
        }

        public int logSettingSave() 
        {
            String fn = System.IO.Directory.GetCurrentDirectory() + System.IO.Path.DirectorySeparatorChar + LogFile.logfile;
            //读 取所有的文件数据
            String all = "";
            int count = 0;
            StreamReader rd;
            Encoding targetEncoding;
            try
            {
                targetEncoding = Unility.TxtFileEncoding.GetEncoding(fn, Encoding.Default);
                rd = new StreamReader(fn, targetEncoding);
            }
            catch (Exception e)
            {
                System.Console.WriteLine("logSettingSave无法打开:" + fn);
                MessageBox.Show("logSettingSave无法打开文件" + fn);
                return -1;
            }

            String str;
            for (str = rd.ReadLine(); str != null; str = rd.ReadLine())
            {
                count ++;
                if(count != this.logline){
                    all += str + "\r\n";
                }else{
                    all += String.Format("setting[{0}][{1}][{2}][{3}][{4}][{5}][{6}]",this.display.ToString(),
                                                            this.mode.ToString(),
                                                            this.times.ToString(),
                                                            this.backcolor.ToString(),
                                                            this.font.ToString(),
                                                            this.fontsize.ToString(),
                                                            this.fontcolor.ToString())  +  "\r\n";
                }
            }

            rd.Close();


            StreamWriter wt;
            try
            {
                wt = new StreamWriter(fn ,false,targetEncoding);
            }
            catch (Exception e)
            {
                System.Console.WriteLine("logSettingSave无法打开:" + fn);
                MessageBox.Show("logSettingSave无法打开文件:" + fn);
                return -1;
            }

            wt.WriteLine(all);

            wt.Close();

            return 0;
        
        }
    
    }



    enum DisplayStep { 
        HanZi = 1,
        JiaMin = 2,
        YuYi = 3,
        LiJu = 4,
    }

    enum LogSettingDisplay { 
        JiaMingFirst = 1,  //假名优先
        HanZiFirst = 2,    //汉字优先
        YuYiFirst = 3,     //语义优先
    }


    enum LogSettingMode {
        RandBrouse = 1,
        RandRecite = 2,

        SequenceBrouse = 3,
        SequenceRecite = 4,
    }


    enum LogSettingTimes { 
        
        Disable = 1,
        Second1 = 2,
        Second2  = 3,
        Second3  = 4,
        Second4 = 5,
        Second5 = 6,
        Second6 = 7,
        
    }






   



}
